<?php

namespace App\Controllers\Order;

use App\Models\Order\PailebaoOrder;
use \App\Libs\Pay\PayLibs;
use \App\Libs\Helper\WeixinHelper;

class AlipayChargePailebao extends \App\Libs\Base\BaseAction
{

    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }

    public function execute()
    {

        $uid = $this->session->user->id;
//        $uid = $this->request->get('uid');
        if (!$this->request->has('num')) {
            return \App\Libs\Views\Json::view(1, '参数不完整:冲多少钱？', NULL);
        }
        $chargeMoney = (float)$this->request->get('num');
        if ($chargeMoney < 0.01) {
            return \App\Libs\Views\Json::view(1, "充值金额有误!至少0.01元哦!", NULL);
        }
		 if (!$this->request->has('pay_type')) {
            return \App\Libs\Views\Json::view(1, '参数不完整', NULL);
        }

		$payType = $this->request->get('pay_type');//支付方式 1.微信app 2.支付宝app
        //根据钱数去产生一个订单，
        //生成一个订单号
        list($usec, $sec) = explode(" ", microtime());
        $usec = $usec * 1000000;
        $order_sn = 'cr' . $sec . $usec . mt_rand(10, 99);

        $chargeOrder = new PailebaoOrder();
        $chargeOrder->uid = $uid;
        $chargeOrder->order_sn = $order_sn;
        $chargeOrder->operate = '1';  //1 代表充值 0：代表消费
        $chargeOrder->money_num = $chargeMoney;  //充值钱数
        $chargeOrder->order_status = '0';  //1 已支付 0：待支付
        $chargeOrder->pay_method = $payType; //标识重试的方式，1：微信，2支付宝;
        $res = $chargeOrder->create();    //
        if (!$res) {
            return \App\Libs\Views\Json::view(1, '充值失败，服务器繁忙，请重试', null);
        }
        //接着去请求支付宝返回APP请求字符串；

        $goodsInfo = [
            'subject' => '派乐宝充值:' . $chargeMoney . '元',
            'out_trade_no' => $order_sn,
            'total_amount' => $chargeMoney,   //订单总金额
            'product_code' => 'QUICK_MSECURITY_PAY',   //固定值
            'passback_params' => urlencode(json_encode([
                'type' => 'charge'   //分辨是充值，还是购买东西
            ]))
        ];
		
		//支付处理
		switch( $payType ){
			case 1:
				//$code = $this->request->get("code");
				$time = time();
				$code = null;
				$prePayId = WeixinHelper::prePay( $code , $goodsInfo['out_trade_no'] , $goodsInfo['subject'] , $goodsInfo['total_amount'] * 100,'charge');
				$nonceStr = \App\Libs\Captcha\Algorithms\Character::instance()->generate(mt_rand(20, 32))['captcha'];
				$payPullStr = array(
					'appid' => WeixinHelper::$appID,
					'partnerid' => WeixinHelper::$mchID,
					'timestamp' => $time,
					'noncestr' => $nonceStr,
					'prepayid' => $prePayId,
					'package' => 'Sign=WXPay'
				);
				ksort($payPullStr);
				$str = urldecode(http_build_query($payPullStr));
				$str .= '&key=' . WeixinHelper::$key;
				$payPullStr['sign'] = strtoupper(md5($str));
				break;
			case 2:
				 //充值的信息，以支付宝返回的为准
				$alipay = new PayLibs(1);
				$payPullStr = $alipay->getAlipayAppStr($goodsInfo);
				break;
			default:
				break;
		}
       
        return \App\Libs\Views\Json::view(0, '成功', $payPullStr);
    }

}
